package kr.co.doriclassic.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.co.doriclassic.mylibrary.ConnectionFactory;
import kr.co.doriclassic.mylibrary.DbUtil;
import kr.co.doriclassic.summary_java.dao.CodeTitleDao;
import kr.co.doriclassic.summary_java.dao.DepartmentDao;
import kr.co.doriclassic.summary_java.dao.EmployeeDao;
import kr.co.doriclassic.summary_java.dao.InterfaceDao;
import kr.co.doriclassic.summary_java.dto.CodeTitle;
import kr.co.doriclassic.summary_java.dto.Department;
import kr.co.doriclassic.summary_java.dto.Employee;



public class CopyOfTestMain extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu menuEmp;
	private JMenu menuDep;
	private JMenu menuTitle;
	private JMenuItem addItemEmp;
	private JMenuItem listItemEmp;
	private JMenuItem addItemDep;
	private JMenuItem listItemDep;
	private JMenuItem addItemTitle;
	private JMenuItem listItemTitle;
	private Connection con; 
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			
			public void run() {
				try {					
					CopyOfTestMain frame = new CopyOfTestMain();
					frame.setVisible(true);					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}

	
	public CopyOfTestMain() {
		initialize();
	}
	private void initialize() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				super.windowClosed(e);
				DbUtil.close(con);
			}
		});
		con = ConnectionFactory.getConnection();

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		
		menuEmp = new JMenu("사원정보");
		menuBar.add(menuEmp);
		
		addItemEmp = new JMenuItem("추가");
		addItemEmp.addActionListener(this);
		menuEmp.add(addItemEmp);
		
		listItemEmp = new JMenuItem("목록");
		listItemEmp.addActionListener(this);
		menuEmp.add(listItemEmp);
		
		menuDep = new JMenu("부서정보");
		menuBar.add(menuDep);
		
		addItemDep = new JMenuItem("추가");
		addItemDep.addActionListener(this);
		menuDep.add(addItemDep);
		
		listItemDep = new JMenuItem("목록");
		listItemDep.addActionListener(this);
		menuDep.add(listItemDep);
		
		menuTitle = new JMenu("직급정보");
		menuBar.add(menuTitle);
		
		addItemTitle = new JMenuItem("추가");
		addItemTitle.addActionListener(this);
		menuTitle.add(addItemTitle);
		
		listItemTitle = new JMenuItem("목록");
		listItemTitle.addActionListener(this);
		menuTitle.add(listItemTitle);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == listItemTitle) {
			actionPerformedListItemTitle(e);
		}
		if (e.getSource() == addItemTitle) {
			actionPerformedAddItemTitle(e);
		}
		if (e.getSource() == listItemDep) {
			actionPerformedListItemDep(e);
		}
		if (e.getSource() == addItemDep) {
			actionPerformedAddItemDep(e);
		}
		if (e.getSource() == listItemEmp) {
			actionPerformedListItemEmp(e);
		}
		if (e.getSource() == addItemEmp) {
			actionPerformedAddItemEmp(e);
		}
	}
	protected void actionPerformedAddItemEmp(ActionEvent e) {
		PanelEmployeetUI emp = new PanelEmployeetUI();
		emp.setDao(new EmployeeDao(con));
		rPaint(emp);	
	}
	protected void actionPerformedListItemEmp(ActionEvent e){
		PanelTable<Employee> tb = new PanelTable<>();
		tb.setLblNewLabel("사원정보");
		tb.setDao(new EmployeeDao(con));
		rPaint(tb);	
	}
	protected void actionPerformedAddItemDep(ActionEvent e) {
		PanelDepartmentUI dep = new PanelDepartmentUI();
		dep.setDao(new DepartmentDao(con));
		rPaint(dep);	
	}
	protected void actionPerformedListItemDep(ActionEvent e) {
		PanelTable<Department> tb = new PanelTable<>();
		tb.setLblNewLabel("부서정보");	
		tb.setDao(new DepartmentDao(con));
		rPaint(tb);			
	}
	
	protected void actionPerformedAddItemTitle(ActionEvent e) {
		PanelCodeTitleUI ct = new PanelCodeTitleUI();
		ct.setDao(new CodeTitleDao(con));
		rPaint(ct);		
	}
	protected void actionPerformedListItemTitle(ActionEvent e) {
		PanelTable<CodeTitle> tb = new PanelTable<>();
		tb.setLblNewLabel("직급정보");
		tb.setDao(new CodeTitleDao(con));
		rPaint(tb);	
	}
	
	private void rPaint(JPanel ui) {
		contentPane.removeAll();
		contentPane.add(ui);
		contentPane.revalidate();		
	}
}
